package com.zlm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zlm.common.Result;
import com.zlm.entity.ShoppingCart;
import com.zlm.service.ShoppingCartService;
import com.zlm.utils.BaseHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加商品到购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public Result<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("shoppingCart: {}", shoppingCart);

        // 1.设置当前购物车的用户id 不然我们不知道是谁买的这个东西
        Long userId = BaseHolder.getCurrentId();
        shoppingCart.setUserId(userId);

        // 2.查询当前菜品或套餐是否存在购物车中
        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, userId);

        if (dishId != null) {
            // 添加到购物车的就是菜品
            // 菜品的id
            lambdaQueryWrapper.eq(ShoppingCart::getDishId, dishId);
            // 还有我们口味
//            lambdaQueryWrapper.eq(ShoppingCart::getDishFlavor, shoppingCart.getDishFlavor());

        } else {
            // 否则是我们的套餐
            // 套餐的id
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        // sql : select * form shopping_cart where user_id = ? and dish_id = ?/setmeal_id = ? ( and dish_flavor = ?) 后面这个我们的菜品的添加
        // 我们只要口味不一样就是新的菜品
        ShoppingCart one = shoppingCartService.getOne(lambdaQueryWrapper);
        if (one != null) {
            // 查询到了 原本就有一样的
            // 3.如果存在 就在原来数量基础上+1
            Integer number = one.getNumber();
            one.setNumber(number + 1);
            shoppingCartService.updateById(one);
        } else {
            // 4.不存在就添加到购物车
            // 就直接新增
            // 第一次就设置为1
            shoppingCart.setNumber(1);
            // 设置订单创建时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }


        return Result.success(one);
    }


    /**
     * 查看购物车
     *
     * @return
     */
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list() {
        // 根据userId来查询就可以了
        log.info("查看购物车");
        Long userId = BaseHolder.getCurrentId();
        // 添加条件
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, userId);
        // 添加排序条件
        lambdaQueryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(lambdaQueryWrapper);

        return Result.success(list);
    }


    /*@PostMapping("/sub")
    public Result reduce(@RequestBody ShoppingCart shoppingCart) {
        // 因为我们不知道他减得是菜品还是套餐所以还是要进行分开来
        Long userId = BaseHolder.getCurrentId();

        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, userId);

        if (dishId != null) {
            // 表明要减少的是我们的菜品
            lambdaQueryWrapper.eq(ShoppingCart::getDishId, dishId);

        } else {
            // 表明要减少我们的套餐
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        // 查询出来
        ShoppingCart shoppingCart1 = shoppingCartService.getOne(lambdaQueryWrapper);

        shoppingCart1.setNumber(shoppingCart1.getNumber() - 1);

        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(ShoppingCart::getNumber, 0);
        ShoppingCart one = shoppingCartService.getOne(lambdaQueryWrapper1);
        if (one != null) {
            shoppingCartService.removeById(one.getId());
        }

        shoppingCartService.updateById(shoppingCart1);

        return Result.success(shoppingCart1);
    }*/

    /**
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public Result<ShoppingCart> subShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据", shoppingCart);
        Long dishId = shoppingCart.getDishId();
        Long currentId = BaseHolder.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);
        if (dishId != null) {
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
        if (cartServiceOne != null && cartServiceOne.getNumber() > 1) {
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number - 1);
            shoppingCartService.updateById(cartServiceOne);
        } else if (cartServiceOne != null) {
            shoppingCartService.removeById(cartServiceOne.getId());
            cartServiceOne.setNumber(0);
        }
        return Result.success(cartServiceOne);
    }


    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public Result<String> clean() {
        // sql : delete from shopping_cart where user_id = ?
        Long userId = BaseHolder.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, userId);
        shoppingCartService.remove(lambdaQueryWrapper);
        return Result.success("清空成功");
    }


}
